package com.xudu.tourbackend.controller.Impl;

import com.xudu.tourbackend.common.DeleteBatchRequest;
import com.xudu.tourbackend.common.Result;
import com.xudu.tourbackend.common.globalEnum.ErrorCode;
import com.xudu.tourbackend.controller.TagController;
import com.xudu.tourbackend.entity.Tag;
import com.xudu.tourbackend.exception.ServiceException;
import com.xudu.tourbackend.service.ITagService;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Collections;
import java.util.List;

/**
 * @className: TagControllerImpl
 * @description: TODO
 * @author: xudu
 * @create: 2024-06-22
 */
@CrossOrigin(origins = "*")
@RestController
public class TagControllerImpl implements TagController {

    @Resource
    private ITagService tagService;

    @Override
    public Result addTag(@RequestBody Tag tag, HttpServletRequest request) {
        String tagName = tag.getTagName();
        Integer i = tagService.addTag(tagName, request);
        return Result.success(i);

    }

    @Override
    public Result deleteTag(@RequestBody DeleteBatchRequest deleteBatchRequest, HttpServletRequest request) {

        List<Integer> ids = deleteBatchRequest.getIds();

        if (ids.isEmpty()){
            throw new ServiceException(ErrorCode.NULL_ERROR);
        }

        int i = tagService.deleteTag(ids, request);
        return Result.success(i);


    }

    @Override
    public Result updateTag(@RequestBody Tag tag, HttpServletRequest request) {
        Integer id = tag.getTagId();
        String tagName = tag.getTagName();

        Boolean b = tagService.updateTag(id, tagName, request);
        return Result.success(b);
    }

    @Override
    public Result searchTagById(Integer id, HttpServletRequest request) {
        if (id == null || id <= 0){
            return null;
        }
        Tag tag = tagService.searchTag(id, request);
        return Result.success(tag);
    }

    @Override
    public Result searchTagByName(String tagName) {
        List<Tag> tags = tagService.searchTag(tagName);
        return Result.success(tags);
    }
}
